﻿/// <reference path="../../utilities/FXDom.js">
///--------------------------------------------------------------------
/// <file name="CustomMessageAutoCompleteEntry.js">
///   Encapsulates a single entry in the AutoComplete UI
///   for a custom message.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.CustomMessageAutoCompleteEntry = function(message, isDefaultMessage) {
    /// <summary>
    ///   Initializes a new instance of the AutoCompleteEntry class.
    /// </summary>
    /// <param name="message" type="nfxICustomMessage">
    ///   The nfxICustomMessage instance around which to wrap an AutoCompleteEntry.
    /// </param>
    /// <param name="isDefaultMessage" type="Boolean">
    ///   Specifies whether the message parameter represents the default message.
    /// </param>
    
    this._message = message;
    this._isDefaultMessage = isDefaultMessage;
};

CustomMessageAutoCompleteEntry.formatTitle = function(title) {
    /// <summary>
    /// Formats the title for display in the AutoComplete list.
    /// </summary>
    /// <param name="title" type="String">
    /// The title to format.
    /// </param>
    /// <returns type="String" />
    
    return "[c:" + title + "]";
}

CustomMessageAutoCompleteEntry.prototype = new AutoCompleteEntry();

CustomMessageAutoCompleteEntry.prototype.addTo = function(textArea, context) {
    /// <summary>
    /// Adds the text associated with this AutoCompleteEntry
    /// to the specified text area, taking into account the
    /// context (i.e. the text behind and ahead of the caret).
    /// </summary>
    /// <param name="textArea" type="TextArea">
    /// The text area to which to add the entry's text.
    /// </param>
    /// <param name="context">
    /// The textual context of the caret. Contains two properties,
    /// previous and following, that provide this information.
    /// </param>
    
    if (textArea.selectionStart === textArea.selectionEnd) {
        var selectionStart = textArea.selectionStart - context.previous.length;
        textArea.setSelectionRange(
            selectionStart,
            textArea.selectionEnd + context.following.length
        );
            
        var editor = textArea.
            QueryInterface($("nsIDOMNSEditableElement")).
            editor;
        
        editor.deleteSelection(0);
    }
    
    Markup.addCustomMessage(this._message.key, textArea);
};

CustomMessageAutoCompleteEntry.prototype.addAttributes = function(listItem) {
    /// <summary>
    ///   Adds attributes to the list item.
    /// </summary>
    /// <param name="listItem">
    ///   The list item element created for the auto-complete element.
    /// </param>
    
    var htmlDocument = listItem.ownerDocument;

    if (!htmlDocument.__namfox__.attachedAutoCompleteMessageObserver) {
        var observerService = $("@mozilla.org/observer-service;1").service("nsIObserverService");
        var observer = {
            init: function() {
                observerService.addObserver(this, "custom-message-name-changed", false);
                observerService.addObserver(this, "quit-application", false);
            },
            
            observe: function(subject, topic, data) {
                switch (topic) {
                    case "quit-application":
                        this.unload();
                        break;
                    case "custom-message-name-changed":
                        var customMessage = subject.QueryInterface($("nfxINamedCustomMessage"));
                        
                        $FX("#namfox-auto-complete-message-" + customMessage.key, htmlDocument).attr(
                            "innerHTML",
                            CustomMessageAutoCompleteEntry.formatTitle(customMessage.name)
                        );
                        break;
                }
            },
            
            unload: function() {
                observerService.removeObserver(this, "custom-message-name-changed");
                observerService.removeObserver(this, "quit-application");
            }
        };
        
        observer.init();
        htmlDocument.defaultView.addEventListener(
            "unload",
            $.createCallback(observer.unload, observer),
            false);

        htmlDocument.__namfox__.attachedAutoCompleteMessageObserver = true;
    }
    
    AutoCompleteEntry.prototype.addAttributes.call(this, listItem);
    
    listItem.id = "namfox-auto-complete-message-" + this._message.key;
};

CustomMessageAutoCompleteEntry.prototype.getDescription = function() {
    /// <summary>
    /// Gets the description associated with this AutoCompleteEntry.
    /// </summary>
    /// <returns type="String" />
    
    return (this._isDefaultMessage ? "<b>Default Custom Message</b>" : "<b>Custom Message </b>" + this._message.name) +
        "<br /><br />" +
        this._message.text;
};
    
CustomMessageAutoCompleteEntry.prototype.getTitle = function() {
    /// <summary>
    /// Gets the entry title associated with this AutoCompleteEntry.
    /// </summary>
    /// <returns type="String" />
    
    var title = this._isDefaultMessage ? "Default" : this._message.name;
    
    return CustomMessageAutoCompleteEntry.formatTitle(title);
};

}});